package org.create.builder;

import org.create.bean.FieldInfo;
import org.create.bean.TableInfo;
import org.create.utils.DataTool;
import org.create.utils.DateTool;
import org.create.utils.IoTool;
import org.create.utils.LogTool;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static org.create.bean.Constants.*;
import static org.create.bean.Constants.JAVA_SUFFIX;


public class BuildController {
    private static BufferedWriter bw;

    private static void buildPackageComment(TableInfo tableInfo) throws IOException {
        String beanName = tableInfo.getBeanName();
        writer("package " + PACKAGE_BASE + "." + PACKAGE_CONTROLLER + ";", 2);
        writer("import " + PACKAGE_BASE + "." + PACKAGE_PO + "." + beanName + ";");
        writer("import " + PACKAGE_BASE + "." + PACKAGE_QUERY + "." + beanName + "Query;");
        writer("import " + PACKAGE_BASE + "." + PACKAGE_SERVICE + "." + beanName + "Service;");
        writer("import " + PACKAGE_BASE + "." + PACKAGE_VO + ".ResponseVo;");
        writer("import org.springframework.web.bind.annotation.RequestBody;");
        writer("import org.springframework.web.bind.annotation.RequestMapping;");
        writer("import org.springframework.web.bind.annotation.RestController;", 2);
        writer("import javax.annotation.Resource;");
        writer("import java.util.List;", 3);
    }

    private static void buildDescriptionComment(TableInfo tableInfo) throws IOException {
        writer("/**");
        writer(" * Description " + tableInfo.getComment() + "控制器");
        writer(" * date " + DateTool.DateFormat(new Date(), YMD_HMS));
        writer(" */");
    }

    private static void buildClassComment(TableInfo tableInfo) throws IOException {
        String beanName = tableInfo.getBeanName();
        writer("@RestController");
        writer("@RequestMapping(\"/" + DataTool.firstChatToLower(beanName) + "\")");
        writer("public class " + beanName + "Controller extends ABaseController {");
        writer("    @Resource");
        writer("    private "+beanName+"Service " + DataTool.firstChatToLower(beanName) + "Service;", 2);
        writer("    @RequestMapping(\"loadDataList\")");
        writer("    public ResponseVo loadDataList(" + beanName + "Query query) {");
        writer("        return getSuccessResponseVo(" + DataTool.firstChatToLower(beanName) + "Service.findListByPage(query));");
        writer("    }", 2);

        buildFieldDescriptionComment("新增");
        writer("    @RequestMapping(\"add\")");
        writer("    public ResponseVo add(" + beanName + " bean) {");
        writer("        " + DataTool.firstChatToLower(beanName) + "Service.add(bean);");
        writer("        return getSuccessResponseVo(null);");
        writer("    }", 2);

        buildFieldDescriptionComment("新增或修改");
        writer("    @RequestMapping(\"addOrUpdate\")");
        writer("    public ResponseVo addOrUpdate(" + beanName + " bean) {");
        writer("        " + DataTool.firstChatToLower(beanName) + "Service.addOrUpdate(bean);");
        writer("        return getSuccessResponseVo(null);");
        writer("    }", 2);

        buildFieldDescriptionComment("批量新增");
        writer("    @RequestMapping(\"addBatch\")");
        writer("    public ResponseVo addBatch(@RequestBody List<" + beanName + "> listBean) {");
        writer("        " + DataTool.firstChatToLower(beanName) + "Service.addBatch(listBean);");
        writer("        return getSuccessResponseVo(null);");
        writer("    }", 2);

        buildFieldDescriptionComment("批量新增或修改");
        writer("    @RequestMapping(\"addOrUpdateBatch\")");
        writer("    public ResponseVo addOrUpdateBatch(@RequestBody List<" + beanName + "> listBean) {");
        writer("        " + DataTool.firstChatToLower(beanName) + "Service.addOrUpdateBatch(listBean);");
        writer("        return getSuccessResponseVo(null);");
        writer("    }", 2);

        for (Map.Entry<String, List<FieldInfo>> entry : tableInfo.getKeyIndexMap().entrySet()) {
            List<FieldInfo> fieldInfoList = entry.getValue();
            StringBuilder sb = new StringBuilder();
            StringBuilder params = new StringBuilder();
            StringBuilder args = new StringBuilder();
            int loop = 1;
            for (FieldInfo fieldInfo : fieldInfoList) {
                sb.append(DataTool.firstChatToUpper(fieldInfo.getPropertyName()));
                params.append(fieldInfo.getJavaType()).append(" ").append(fieldInfo.getPropertyName());
                args.append(fieldInfo.getPropertyName());
                if (loop < fieldInfoList.size()) {
                    loop++;
                    sb.append("And");
                    params.append(", ");
                    args.append(", ");
                }
            }
            buildFieldDescriptionComment("根据" + sb + "查询");
            writer("    @RequestMapping(\"get" + beanName + "By" + sb + "\")");
            writer("    public ResponseVo get" + beanName + "By" + sb + "(" + params + ") {");
            writer("        return getSuccessResponseVo(" + DataTool.firstChatToLower(beanName) + "Service.get" + beanName + "By" + sb + "(" + args + "));");
            writer("    }", 2);
            buildFieldDescriptionComment("根据" + sb + "修改");
            writer("    @RequestMapping(\"update" + beanName + "By" + sb + "\")");
            writer("    public ResponseVo update" + beanName + "By" + sb + "(" + beanName + " bean, " + params + ") {");
            writer("        " + DataTool.firstChatToLower(beanName) + "Service.update" + beanName + "By" + sb + "(bean, " + args + ");");
            writer("        return getSuccessResponseVo(null);");
            writer("    }", 2);
            buildFieldDescriptionComment("根据" + sb + "删除");
            writer("    @RequestMapping(\"delete" + beanName + "By" + sb + "\")");
            writer("    public ResponseVo delete" + beanName + "By" + sb + "(" + params + ") {");
            writer("        " + DataTool.firstChatToLower(beanName) + "Service.delete" + beanName + "By" + sb + "(" + args + ");");
            writer("        return getSuccessResponseVo(null);");
            writer("    }");
        }
        writer("}");
    }

    private static void buildFieldDescriptionComment(String description) throws IOException {
        writer("    /**");
        writer("     * " + description);
        writer("     */");
    }

    private static void writer(String str) throws IOException {
        writer(str, 1);
    }

    private static void writer(String str, int num) throws IOException {
        bw.write(str);
        if (num > 0) {
            appendLine(num);
        }
    }

    private static void appendLine(int loop) throws IOException {
        if (loop == 1) {
            bw.newLine();
        } else {
            for (int i = 0; i < loop; i++) {
                bw.newLine();
            }
        }
    }

    public static void run(List<TableInfo> tableInfoList) {
        for (TableInfo tableInfo : tableInfoList) {
            File file = new File(PATH_CONTROLLER + tableInfo.getBeanName() + "Controller" + JAVA_SUFFIX);
            File dir = file.getParentFile();
            boolean d = dir.mkdirs();
            LogTool.printLog(d, "新建目录", dir);
            try {
                boolean r = file.createNewFile();
                LogTool.printLog(r, "新建文件", file);
                bw = IoTool.createBw(file);
                buildPackageComment(tableInfo);
                buildDescriptionComment(tableInfo);
                buildClassComment(tableInfo);
            } catch (Exception e) {
                LogTool.printError(e);
            } finally {
                IoTool.close();
            }
        }
    }
}
